/**
 * 路由生成器 - 负责动态添加路由
 */

import type { RouteGenerateOptions } from '@/types/router'
import { BASIC_LAYOUT } from '@/constants/router'

/**
 * 生成并添加路由到路由实例
 * @param options 路由生成选项
 */
export const generateRoute = (options: RouteGenerateOptions): void => {
  const { router, accessRoutes } = options
  
  // 将权限路由添加到基础布局下
  accessRoutes.forEach((route) => {
    router.addRoute(BASIC_LAYOUT, route)
  })
}

/**
 * 清除动态路由（用于登出等场景）
 * @param router 路由实例
 * @param routeNames 要清除的路由名称数组
 */
export const clearDynamicRoutes = (router: RouteGenerateOptions['router'], routeNames: string[]): void => {
  routeNames.forEach((name) => {
    if (router.hasRoute(name)) {
      router.removeRoute(name)
    }
  })
}
